/***
 * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.com.caelum.vraptor.blank;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.mundolivre.mail.ThreadEmail;
import br.com.mundolivre.model.Email;



@Resource
public class IndexController {

	private final Result result;
	private final Atualizacao atualizacao;
	private final Validator validator;
	HttpServletResponse response;

	public IndexController(Result result, Atualizacao atualizacao, HttpServletResponse response, Validator validator) {
		this.result = result;
		this.atualizacao = atualizacao;
		this.response = response;
		this.validator = validator;
	}

	@Path("/")
	public void index() {
		result.include("variable", "VRaptor!");
	}
	
	@Path("/softwares")
	public void softwares(){
		
	}
	
	@Path("/relogios")
	public void relogios(){
		
	}
	
	@Path("/controle_de_acesso")
	public void controle(){
		
	}
	

	@Path("/pdf/{nome}")
	public File exibrPdf(String nome){		
		
		List<Pdf> lista = atualizacao.lista();
		Pdf retornoPdf = null;
		for(Pdf pdf:lista){
			if(pdf.getNome().equals(nome)){
				retornoPdf = pdf;
			}			
		}		
		//Pdf pdf = lista.get(posicao);
		response.setContentType("application/pdf");
		return new File(retornoPdf.getCaminho());
	}
	
	@Path("/institucional")
	public void empresa(){
		
	}
	
	@Path("/acessorios")
	public void acessorios(){
		
	}
	
	@Path("/login")
	public void login(){
		
	}
	
	
	@Path("/contato")
	public void contato(){
		
	}
	
	@Post("/email")
	public void enviar_email(Email email){
		Thread thread = new ThreadEmail(email);
		thread.start();
		//result.redirectTo(this).contato();
		validator.add(new ValidationMessage("E-mail enviado com sucesso!", ""));
		validator.onErrorUsePageOf(this).index();
	}

}
